<?php
/**
 * Description of TaskAction
 *
 * @author 132
 */
class TaskAction extends TeamBaseAction
{

    private $TASK_STATUS = array('删除', '待认领', '进行中', '已提交', '已验收');

    public function _empty() {
        $tpl = "../Include/App/Group/Bridge/Tpl/Public_404.html";
        $this->display($tpl);
    }

    public function __construct()
    {
        parent::__construct();
        $this->assign('on', 2);
    }

    //put your code here
    public function index()
    {
        $this->assign('mon', 1);

        $table = array(
            'xia_renwu' => 'renwu',
            'xia_user_info' => 'user'
        );
        $field = 'renwu.*,user.photo,user.nickname';
        $mytasks = M('renwu')->table($table)->field($field)->where('renwu.roleid=' . $this->uid . ' and renwu.roleid=user.uid and status>0')->select();
        if ($mytasks) {
            $this->assign('mytasks', $mytasks);
        }
        $othertasks = M('renwu')->table($table)->field($field)->where('renwu.senderid=' . $this->uid . ' and renwu.roleid=user.uid and status>0')->select();
        if ($othertasks) {
            $this->assign('othertasks', $othertasks);
        }

        //dump($othertasks);
        //exit;
        $this->dataheader();
        $this->display();
    }

    //修改任务状态
    public function setTaskstatus_ajax()
    {
        $id = I('id');
        $status = I('status');
        if ($status < 1 || $status > 4) {
            echo json_encode(array('success' => 0, 'msg' => '所提交的状态不合法！'));
            exit;
        }

        //权限校验
        $msg = '无此操作权限';
        $ret = parent::isTeamCreater($this->userinfo['teamid']);
        if (!$ret) {
            $ret = parent::isProjectCreater($this->userinfo['projectid']);
            if (!$ret) {
                //是否任务执行者
                $resTask = M('renwu')->where('id=' . $id)->find();
                if ($resTask['roleid'] != $this->uid) {
                    $ret = parent::teamauthcheck($this->uid, '74');
                    if (!$ret) {
                        $this->ajaxf($msg);
                    }
                } else {
                    if ($status == 4) {
                        ajaxf('任务执行者自己不能验收任务!');
                    }
                }
            }
        }

        $data = array(
            'status' => $status
        );
        $where['id'] = array('eq', $id);
        $res = M('renwu')->where($where)->save($data);
        if ($res) {
            //发送情报
            $now = time();
            $resTask = M('renwu')->where('id='.$id)->find();
            $content = "<td>任务<b>[".summary($resTask['content'], 6)."]</b>已被<a href=\"javacript:void(0);\" data-url=\"".genTalentURL($this->uid)."\"><b>[".$this->userinfo['nickname']."]</b></a>设置成<b>[".$this->TASK_STATUS[$status]."]</b>状态</td>";
            $taskcardurl = U("Task/taskcard2@".getDomain(),array('card'=>$resTask['depamentid']));
            $loglist = array();
            $loglist[] = array(
                'uid' => $resTask['senderid'],
                'content' => logPost($content, $taskcardurl, $now),
                'ctime' => $now,
            );
            $loglist[] = array(
                'uid' => $resTask['roleid'],
                'content' => logPost($content, $taskcardurl, $now),
                'ctime' => $now,
            );
            M('office_log')->addAll($loglist);
            //发送邮件
            $body = "任务[" . $resTask['content'] . "]已被[" . $this->userinfo['nickname'] . "]设置成[" . $this->TASK_STATUS[$status] . "]状态";
            $body = mailPostfix($body);
            $resUser = M('user')->where('id=' . $resTask['senderid'] . ' or id=' . $resTask['roleid'])->join('xia_user_info uif on xia_user.id=uif.uid')->select();
            foreach ($resUser as $user) {
                mailqueue_push($user['email'], $user['nickname'], '任务状态被修改', $body);
            }

            echo json_encode(array('success' => 1, 'msg' => ''));
            exit;
        } else {
            echo json_encode(array('success' => 0, 'msg' => '修改任务状态失败！'));
            exit;
        }
    }

    public function taskcard()
    {
        $this->assign('mon', 2);

        $resCard = M('taskcard')->table(array('xia_taskcard' => 'tc', 'xia_user_info' => 'uif'))->field('tc.*,uif.nickname')->where('tc.pid=' . $this->userinfo['projectid'] . ' and state>0 and tc.uid=uif.uid')->select();
        if ($resCard) {
            $this->assign('cards', $resCard);
        }

        //权限校验
        $adder = 0; //添加任务卡片权限
        $deler = 0; //删除任务卡片权限
        $resTeam = parent::isTeamCreater($this->userinfo['teamid']); //M('team')->where('uid='.$this->uid.' and state>0')->find();
        if ($resTeam) {
            $adder = 1;
            $deler = 1;
        }
        $resPrj = parent::isProjectCreater($this->userinfo['projectid']); //M('project')->where('uid='.$this->uid)->find();
        if ($resPrj) {
            $adder = 1;
            $deler = 1;
        }
        $resAuth = parent::teamauthcheck($this->uid, 70); //M('authdispatch')->where('arid='.$this->userinfo['role'].' and authid=70')->find();
        if ($resAuth) {
            $adder = 1;
            $deler = 2;
        }
        $this->assign('adder', $adder);
        $this->assign('deler', $deler);
        $this->dataheader();
        $this->display();
    }

    //添加任务卡
    public function addTaskCard_ajax()
    {
        //是否已有项目
        if ($this->userinfo['projectid'] <= 0) {
            ajaxf('尚未创建项目，不能创建任务卡');
        }

        import('ORG.Util.Verify');
        if (Verify::isEmpty(I('name')) == false) {
            echo json_encode(array('success' => 0, 'msg' => '参数错误!'));
            exit;
        }
        $name = I('name');

        //权限校验
        $adder = 0; //添加部门任务卡片权限
        $resTeam = parent::isTeamCreater($this->userinfo['teamid']); //M('team')->where('uid='.$this->uid.' and state>0')->find();
        if ($resTeam) {
            $adder = 1;
        }
        $resPrj = parent::isProjectCreater($this->userinfo['projectid']); //M('project')->where('uid='.$this->uid)->find();
        if ($resPrj) {
            $adder = 1;
        }
        $resAuth = parent::teamauthcheck($this->uid, 70); //M('authdispatch')->where('arid='.$this->userinfo['role'].' and authid=70')->find();
        if ($resAuth) {
            $adder = 1;
        }
        if ($adder == 0) {
            echo json_encode(array('success' => 0, 'msg' => '您无权限做此操作!'));
            exit;
        }

        $resTaskcard = M('taskcard')->where('pid=' . $this->userinfo['projectid'] . " and name='" . $name . "' and state=1")->find();
        if ($resTaskcard) {
            echo json_encode(array('success' => 0, 'msg' => '已经存在同名卡片!'));
            exit;
        }

        $card = array(
            'name' => $name,
            'pid' => $this->userinfo['projectid'],
            'uid' => $this->uid,
        );

        $res = M('taskcard')->add($card);
        if ($res) {
            echo json_encode(array('success' => 1, 'cardid' => $res, 'creater' => $this->userinfo['nickname']));
            exit;
        } else {
            echo json_encode(array('success' => 0, 'msg' => '添加失败！'));
            exit;
        }
    }

    //删除任务卡
    public function delTaskCard_ajax()
    {
        //测试代码
//        $ret = M('renwu')->where('depamentid=-1')->setField('status', 0);
//        $this->ajaxf('测试');
        $id = I('id');
        //权限校验
        $deler = 0; //添加部门任务卡片权限
        $resTeam = parent::isTeamCreater($this->userinfo['teamid']);
        if ($resTeam) {
            $deler = 1;
        }
        $resPrj = parent::isProjectCreater($this->userinfo['projectid']);
        if ($resPrj) {
            $deler = 1;
        }
        $resAuth = parent::teamauthcheck($this->uid, 70);
        $resTaskcard = M('taskcard')->where('id=' . $id)->find();
        if ($resAuth && $resTaskcard['uid'] == $this->uid) {
            $deler = 1;
        }
        if ($deler == 0) {
            echo json_encode(array('success' => 0, 'msg' => '您无权限做此操作!'));
            exit;
        }

        //将任务卡置为删除
        $tcTab = M('taskcard');
        $resCard = $tcTab->where('id=' . $id)->find();
        $res = $tcTab->where('id=' . $id)->setField('state', 0);
        if ($res) {
            $resTask = M('renwu')->where('depamentid=' . $id . ' and status<>0')->select();
            $ret = M('renwu')->where('depamentid=' . $id)->setField('status', 0);
            if ($ret > 0) {
                $senderArr = false;
                $exerArr = false;
                $allArr = array();
                foreach ($resTask as $task) {
                    if ($task['senderid'] != $task['roleid']) {
                        $senderArr[] = array('senderid' => $task['senderid'], 'name' => summary($task['content'], 6));
                        $exerArr[] = array('roleid' => $task['roleid'], 'name' => summary($task['content'], 6));
                    } else {
                        $senderArr[] = array('senderid' => $task['senderid'], 'name' => $task['name']);
                    }
                }
                $loglist = array();
                $now = time();
                $idArr = array(); //记录id数组，为发送邮件做准备
                foreach ($senderArr as $sender) {
                    $contentS = "<td>您的任务<b>[" . $sender['name'] . "]</b>所属任务卡<b>[" . $resCard['name'] . "]</b>被<b>[" . $this->userinfo['nickname'] . "]</b>删除了,所以该任务也被删除。</td>";
                    array_push($idArr, 'id=' . $render['senderid']);
                    $loglist[] = array(
                        'uid' => $sender['senderid'],
                        'content' => logPost($contentS, "", $now),
                        'ctime' => $now,
                    );
                }
                foreach ($exerArr as $exer) {
                    $contentE = "<td>您被分配的任务<b>[" . $exer['name'] . "]</b>所属任务卡<b>[" . $resCard['name'] . "]</b>被<b>[" . $this->userinfo['nickname'] . "]</b>删除了,所以该任务也被删除。</td>";
                    array_push($idArr, 'id=' . $exer['roleid']);
                    $loglist[] = array(
                        'uid' => $exer['roleid'],
                        'content' => logPost($contentE, "", $now),
                        'ctime' => $now,
                    );
                }
                M('office_log')->addAll($loglist);
                //发送邮件
                //拼接id字段
                $idstr = impolde(' or ', $idArr);
                $resUser = M('user')->where($idstr)->join('xia_user_info uif on xia_user.id=uif.uid')->select();
                $contentS = mailPostfix($contentS);
                foreach ($resUser as $user) {
                    mailqueue_push($user['email'], $user['nickname'], '任务卡被删除', $contentS);
                }
            }
            //向删除任务卡的人发送任务卡被删除的情报
            $content = "<td>任务卡<b>[" . $resCard['name'] . "]</b>已被删除.</td>";
            $content = logPost($content, "", time(), "设为已读");
            sendNotice($this->uid, $content);
            echo json_encode(array('success' => 1, 'msg' => ''));
            exit;
        }
        echo json_encode(array('success' => 0, 'msg' => '删除失败!'));
        exit;
    }

    public function taskcard2()
    {
        import('ORG.Util.Verify');
        if (Verify::isEmpty(I('card')) == false) {
            $this->redirect('/@.'.getDomain());
            exit;
        }

        $this->assign('mon', 2);

        $cardid = I('card');
        $resCard = M('taskcard')->where('id=' . $cardid)->find();
        if ($resCard) {
            $this->assign('card', $resCard);
            $resRenwu = M('renwu')->table('xia_renwu as renwu,xia_user_info as user')->field('renwu.*,user.nickname,user.photo')->where('renwu.depamentid=' . $cardid . ' and status>0 and renwu.roleid=user.uid')->select();
            if ($resRenwu) {
                $this->assign('taskList', $resRenwu);
            }
        }

        //权限校验
        $adder = 0; //添加任务权限
        $deler = 0; //删除任务权限
        $statuser = 0; //任务状态修改权限
        $resTeam = parent::isTeamCreater($this->userinfo['teamid']); //M('team')->where('uid='.$this->uid.' and state>0')->find();
        if ($resTeam) {
            $adder = 1;
            $deler = 1;
            $statuser = 1;
        }
        $resPrj = parent::isProjectCreater($this->userinfo['projectid']); //M('project')->where('uid='.$this->uid)->find();
        if ($resPrj) {
            $adder = 1;
            $deler = 1;
            $statuser = 1;
        }
        $resAuth = parent::teamauthcheck($this->uid, 72); //M('authdispatch')->where('arid='.$this->userinfo['role'].' and authid=72')->find();
        if ($resAuth) {
            $adder = 1;
        }
        $resAuth = false;
        $resAuth = parent::teamauthcheck($this->uid, 75); //M('authdispatch')->where('arid='.$this->userinfo['role'].' and authid=75')->find();
        if ($resAuth) {
            $deler = 1;
        }
        $resAuth = false;
        $resAuth = parent::teamauthcheck($this->uid, 76); //M('authdispatch')->where('arid='.$this->userinfo['role'].' and authid=76')->find();
        if ($resAuth) {
            $statuser = 1;
        }
        $this->assign('adder', $adder);
        $this->assign('deler', $deler);
        $this->assign('statuser', $statuser);
        $this->dataheader();
        $this->display();
    }

    //部门成员列表ajax接口
    public function members_ajax()
    {
        import('ORG.Util.Verify');
        $chooseDepMember = Verify::isEmpty(I('did'));
        if ($chooseDepMember) {
            //只选择部门成员
            $did = I('did');
            $resMembers = M('job_user')->table('xia_job_user as jobuser,xia_user_info as user')->field('jobuser.*,user.nickname,user.photo')->where('did=' . $did . ' and jobuser.uid=user.uid')->select();
            echo json_encode($resMembers);
            exit;
        } else {
            //用户所在项目的所有成员,包括项目创建者,已申请离职或发起辞退流程的不算在内
            $resMembers = M('job')->table('xia_job as job,xia_job_user as job_user,xia_user_info as user')->field('user.uid as userid,user.nickname,user.photo')->where('job.state>0 and job.pid=' . $this->userinfo['projectid'] . ' and job.id=job_user.jobid and job_user.state=2 and job_user.uid=user.uid')->select();
            $resPrj = M('project')->where('id=' . $this->userinfo['projectid'] . ' and state>0')->find();
            $resUif = M('user_info')->where('uid=' . $resPrj['uid'])->find(); //项目创建者
            $resMembers[] = array(
                'userid' => $resPrj['uid'],
                'nickname' => $resUif['nickname'],
                'photo' => $resUif['photo'],
            );
            echo json_encode($resMembers);
            exit;
        }
    }

    //添加任务
    public function addTask_ajax()
    {
        //权限校验
        $adder = 0; //添加任务权限
        $resTeam = parent::isTeamCreater($this->userinfo['teamid']); //M('team')->where('uid='.$this->uid.' and state>0')->find();
        if ($resTeam) {
            $adder = 1;
        }
        $resPrj = parent::isProjectCreater($this->userinfo['projectid']); //M('project')->where('uid='.$this->uid)->find();
        if ($resPrj) {
            $adder = 1;
        }
        $resAuth = parent::teamauthcheck($this->uid, 72); //M('authdispatch')->where('arid='.$this->userinfo['role'].' and authid=72')->find();
        if ($resAuth) {
            $adder = 1;
        }
        if ($adder == 0) {
            echo json_encode(array('success' => 0, 'msg' => '您无权限添加任务'));
            exit;
        }

        $content = I('content');
        $memberid = I('member');
        $rolename = I('rolename');
        $deadline = I('deadline');
        $pid = I('prj'); //废弃，从$resPrj取
        $cardid = I('card');
        $cardname = I('cardname');

        $now = time();
        $data = array(
            'name' => '',
            'content' => $content,
            'senderid' => $this->uid,
            'roleid' => $memberid,
            'rolename' => $rolename,
            'deadline' => $deadline,
            'pid' => $this->userinfo['projectid'],
            'depamentid' => $cardid,
            'depament' => $cardname,
            'status' => 1,
            'ctime' => $now
        );

        $taskTab = M('renwu');
        $resTask = $taskTab->add($data);
        if ($resTask) {
            //发送情报给执行者
            $content = "<td><a href='" . genTalentURL($this->uid) . "'><b>[" . $this->userinfo['nickname'] . "]</b></a>向您分派了一个任务，该任务在<b>[" . $cardname . "]</b></a>任务卡中，请尽快领取。</td>";
            $content = logPost($content, U("Task/taskcard2@".getDomain(), array('card' => $cardid)), $now, "去看看");
            sendNotice($memberid, $content);
            //发送邮件
            $body = "[" . $this->userinfo['nickname'] . "]向您分派了一个任务，该任务在[" . $cardname . "]任务卡中，请尽快领取。";
            $body = mailPostfix($body);
            $resUser = M('user')->where('id=' . $memberid)->join('xia_user_info uif on xia_user.id=uif.uid')->find();
            mailqueue_push($resUser['email'], $resUser['nickname'], '您有一个任务', $body);

            echo json_encode(array('success' => 1));
            exit;
        } else {
            ajaxf($taskTab->getLastSql());
        }
    }

    //编辑任务
    public function editTask_ajax()
    {
        //权限校验
        $adder = 0; //编辑任务权限
        $resTeam = parent::isTeamCreater($this->userinfo['teamid']); //M('team')->where('uid='.$this->uid.' and state>0')->find();
        if ($resTeam) {
            $adder = 1;
        }
        $resPrj = parent::isProjectCreater($this->userinfo['projectid']); //M('project')->where('uid='.$this->uid)->find();
        if ($resPrj) {
            $adder = 1;
        }
        $taskid = I('task');
        if (!$taskid) {
            ajaxf("参数错误！");
        }
        $taskTab = M('renwu');
        $resTask = $taskTab->where('id=' . $taskid)->find(); //只有待认领的任务可以修改内容,只有任务发布者可以修改
        if (!$resTask) {
            ajaxf("参数错误！");
        }
        if ($resTask['status'] != 1) {
            ajaxf("当前无此权限！");
        }
        if (adder == 0 && $resTask['senderid'] == $this->uid) {
            $adder = 1;
        }
        if ($adder == 0) {
            ajaxf("很遗憾，您无权限修改这个任务！");
        }

        $content = I('content');
        $memberid = I('member');
        $rolename = I('rolename');
        $deadline = I('deadline');
        if (!$content || !$rolename || !$deadline) {
            ajaxf("参数错误！");
        }
        $data = array(
            'id' => $taskid,
            'content' => $content,
            'roleid' => $memberid,
            'rolename' => $rolename,
            'deadline' => $deadline,
        );
        $taskTab->save($data);
        ajaxs(1, "已保存任务信息！");
    }

    //获取单个任务
    public function getTask_ajax()
    {
        $taskid = I('task');
        $table = 'xia_renwu as renwu,xia_user_info as user';
        $field = 'renwu.*,user.nickname as sender';
        $where = 'renwu.id=' . $taskid . ' and renwu.pid=' . $this->userinfo['projectid'] . ' and renwu.senderid=user.uid';
        $task = M('renwu')->table($table)->field($field)->where($where)->select();
        if ($task) {
            echo json_encode(array('success' => 1, 'data' => $task));
            exit;
        } else {
            ajaxf();
        }
    }

    public function taskprogress()
    {
        $this->assign('mon', 3);

        $taskTotal = array(
            1 => array('count' => 0, 'percent' => 0),
            2 => array('count' => 0, 'percent' => 0),
            3 => array('count' => 0, 'percent' => 0),
            4 => array('count' => 0, 'percent' => 0)
        );
        //任务
        $a = $b = $c = $d = 0;
        $tasks = array();
        $resTask = M('renwu')->where('pid=' . $this->userinfo['projectid'] . ' and status>0')->order('ctime desc')->select();
        foreach ($resTask as $task) {
            $status = $task['status'];
            $taskTotal[$status]['count'] = $taskTotal[$status]['count'] + 1;

            if ($status == 1) {
                $a++;
            } else if ($status == 2) {
                $b++;
            } else if ($status == 3) {
                $c++;
            } else if ($status == 4) {
                $d++;
            }
            $deadtime = strtotime($task['deadline']);
            $ym = date('Y.m月', $deadtime);
            if (($a == 1 && $status == 1)
                || ($b == 1 && $status == 2)
                || ($c == 1 && $status == 3)
                || ($d == 1 && $status == 4)
                || $tasks[$status]['expend']['mon'] == $ym
            ) {
                $tasks[$status]['expend']['mon'] = $ym;
                $tasks[$status]['expend']['list'][] = $task;
            } else {
                $tasks[$status]['ar'][$ym][] = $task;
            }
        }

//        dump($tasks);
//        exit;

        $this->assign('taskTotal', $taskTotal);
        $this->assign('deptaskList', $tasks);
        $this->dataheader();
        $this->display();
    }

    //删除任务
    public function deltask_ajax()
    {
        $taskid = I('task');
        if (!$taskid || $taskid <= 0) {
            echo json_encode(array('success' => 0, 'msg' => '参数错误!'));
            exit;
        }

        //权限校验
        $deler = 0;
        $ret = parent::isTeamCreater($this->userinfo['teamid']);
        if ($ret) {
            $dler = 1;
        }
        $ret = parent::isProjectCreater($this->userinfo['projectid']);
        if ($ret) {
            $dler = 1;
        }
        $resTask = M('renwu')->where('id=' . $taskid)->find();
        if ($resTask['senderid'] == $this->uid && parent::teamauthcheck($this->uid, '71')) { //是创建者而且具备删除任务权限
            $dler = 1;
        }
        if ($dler == 0) {
            $this->ajaxf('无权限删除任务!');
        }


        //是否已经领取，在进行中、已完成状态中的任务不能被删除
        $resTask = M('renwu')->where('id=' . $taskid)->find();
        if ($resTask['status'] == 2 || $resTask['status'] == 3) {
            $this->ajaxf('处于进行中或已完成状态中的任务不能被删除');
        }


        $ret = M('renwu')->where('id=' . $taskid)->setField('status', 0);
        if ($ret) {
            //发送情报
            $content = '<td>任务<b>['.$resTask['content'].']</b>已被<a href="javascript:void(0);" data-url="'.genTalentURL($this->uid).'"><b>['.$this->userinfo['nickname'].']</b></a>删除。</td>';
            $content = logPost($content, "", time());
            sendNotice($resTask['roleid'], $content);
            sendNotice($resTask['senderid'], $content);
            //发送邮件
            $body = '任务[' . $resTask['content'] . ']已被[' . $this->userinfo['nickname'] . ']删除。';
            $body = mailPostfix($body);
            $resUser = M('user')->where('id=' . $resTask['roleid'] . ' or id=' . $resTask['senderid'])->join('xia_user_info uif on xia_user.id=uif.uid')->select();
            foreach ($resUser as $user) {
                mailqueue_push($user['email'], $user['nickname'], '您的任务被删除', $body);
            }

            echo json_encode(array('success' => 1, 'msg' => '已删除任务!'));
            exit;
        } else {
            echo json_encode(array('success' => 0, 'msg' => '删除失败!'));
            exit;
        }
    }

    //任务列表数据通用接口
    public function tasklist_ajax()
    {
        import('ORG.Util.Verify');
        if (Verify::isEmpty(I('type')) == false) {
            echo json_encode(array('success' => 0, 'msg' => '参数错误!'));
            exit;
        }

        $type = I('type'); //1别人分派给我的，2我分派给别人的，3任务卡里的
        $table = array(
            'xia_renwu' => 'renwu',
            'xia_user_info' => 'user'
        );
        $field = 'renwu.*,user.nickname as sender';
        $sender = "renwu.roleid";
        if ($type == 1) {
            $sender = "renwu.roleid";
        }
        if ($type == 2) {
            $sender = "renwu.senderid";
        }
        if ($type == 1 || $type == 2) {
            $where = $sender . '=' . $this->uid . ' and pid=' . $this->userinfo['projectid'] . ' and renwu.senderid=user.uid and status=';
            $status_tasks1 = M('renwu')->table($table)->field($field)->where($where . '1')->select();
            $status_tasks2 = M('renwu')->table($table)->field($field)->where($where . '2')->select();
            $status_tasks3 = M('renwu')->table($table)->field($field)->where($where . '3')->select();
            $status_tasks4 = M('renwu')->table($table)->field($field)->where($where . '4')->select();

            $data = array();
            if ($status_tasks1) {
                $data["status1"] = $status_tasks1;
            }
            if ($status_tasks2) {
                $data["status2"] = $status_tasks2;
            }
            if ($status_tasks3) {
                $data["status3"] = $status_tasks3;
            }
            if ($status_tasks4) {
                $data["status4"] = $status_tasks4;
            }
            $results = array('success' => 1, 'data' => $data);
            echo json_encode($results);
            exit;
        }

        if ($type == 3) {
            if (Verify::isEmpty(I('card')) == false) {
                echo json_encode(array('success' => 0, 'msg' => '参数错误!'));
                exit;
            }
            $cardid = I('card');
            //$resRenwu = M('renwu')->table('xia_renwu as renwu,xia_user_info as user')->field('renwu.*,user.nickname,user.photo') ->where('renwu.depamentid='.$cardid.' and status>0 and renwu.roleid=user.uid')->select();
            $table = 'xia_renwu as renwu,xia_user_info as user';
            $field = 'renwu.*,user.nickname as sender';
            $where = 'renwu.depamentid=' . $cardid . ' and renwu.senderid=user.uid and status=';
            $status_tasks1 = M('renwu')->table($table)->field($field)->where($where . '1')->select();
            $status_tasks2 = M('renwu')->table($table)->field($field)->where($where . '2')->select();
            $status_tasks3 = M('renwu')->table($table)->field($field)->where($where . '3')->select();
            $status_tasks4 = M('renwu')->table($table)->field($field)->where($where . '4')->select();
            $data = array();
            if ($status_tasks1) {
                $data["status1"] = $status_tasks1;
            }
            if ($status_tasks2) {
                $data["status2"] = $status_tasks2;
            }
            if ($status_tasks3) {
                $data["status3"] = $status_tasks3;
            }
            if ($status_tasks4) {
                $data["status4"] = $status_tasks4;
            }
            $results = array('success' => 1, 'data' => $data);
            echo json_encode($results);

            exit;
        }
    }

    public function main()
    {
        $resMembers = M('job')->table('xia_job as job,xia_job_user as job_user,xia_user_info as user')->field('user.uid as userid,user.nickname,user.photo')->where('job.state>0 and job.pid=' . $this->userinfo['projectid'] . ' and job.id=job_user.jobid and job_user.state=2 and job_user.uid=user.uid')->select();
        $resPrj = M('project')->where('id=' . $this->userinfo['projectid'] . ' and state>0')->find();
        $resUif = M('user_info')->where('uid=' . $resPrj['uid'])->find(); //项目创建者
        $resMembers[] = array(
            'userid' => $resPrj['uid'],
            'nickname' => $resUif['nickname'],
            'photo' => $resUif['photo'],
        );
        $this->assign("members", $resMembers);
        $this->assign("status_list", array('未开始', '进行中', '已提交', '已验收'));
        $this->assign("uid", $this->uid);
        $this->dataheader();
        $this->display();
    }

    public function statistics()
    {
        $tabTask = M('prjtask');
        $pid = $this->userinfo['projectid'];
        //项目内任务总数
        $total = $tabTask->where("prjid=$pid")->count();
        $this->assign("total", $total);

        $table = array('xia_prjtask' => 't');
        $field = "t.*,u1.nickname as sender,u2.nickname as exetor,";
        $field .= "(SELECT GROUP_CONCAT(towho) FROM xia_taskat WHERE t.id = xia_taskat.taskid) as cc ";

        $join = "LEFT JOIN xia_user_info u1 ON t.fromwho=u1.uid LEFT JOIN xia_user_info u2 ON t.exer=u2.uid";

        $notDoneTasks = $tabTask->table($table)->field($field)->join($join)->where("t.status=1")->select();
        $processingTasks = $tabTask->table($table)->field($field)->join($join)->where("t.status=2")->select();
        $submittedTasks = $tabTask->table($table)->field($field)->join($join)->where("t.status=3")->select();
        $completedTasks = $tabTask->table($table)->field($field)->join($join)->where("t.status=4")->select();


        $this->assign("notDoneTasks", $this->getResultFromQuery($notDoneTasks));
        $this->assign("processingTasks", $this->getResultFromQuery($processingTasks));
        $this->assign("submittedTasks", $this->getResultFromQuery($submittedTasks));
        $this->assign("completedTasks", $this->getResultFromQuery($completedTasks));

        $this->assign("notDone", count($notDoneTasks));
        $this->assign("processing", count($processingTasks));
        $this->assign("submitted", count($submittedTasks));
        $this->assign("completed", count($completedTasks));

        $this->dataheader();
        $this->display();
    }

    private function getResultFromQuery($data)
    {
        $results = array();
        if (!isset($data) || !is_array($data)) {
            return $results;
        }

        foreach ($data as $item) {
            $row = array();
            $row["id"] = $item["id"];
            $row["title"] = $item["title"];
            $row["sender"] = $item["sender"];
            $row["exetor"] = $item["exetor"];
            $row["exer"] = $item["exer"];
            $row["fromwho"] = $item["fromwho"];
            $row["cc"] = array();
            if (isset($item["cc"]) && !empty($item["cc"])) {
                $row["cc"] = explode(',', $item["cc"]);
            }

            $results[] = $row;
        }
        return $results;
    }

}